﻿Imports System.IO
Imports System.Net
Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1
    Private currentIndex As Integer = -1
    Private totalStocks As Integer = 0
    Private names As New ArrayList


    Private stockbytes As ArrayList

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        stockbytes = New ArrayList
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If names.Count > 0 Then
            For i As Integer = 0 To names.Count - 1
                If names(i) = TextBox1.Text Then
                    currentIndex = i
                    PictureBox1.Image = Image.FromStream(New MemoryStream(DirectCast(stockbytes(currentIndex), Byte())))
                    Label2.Text = currentIndex + 1 & "/" & totalStocks
                    Label1.Text = "Stock: " & names(currentIndex)
                    Exit Sub
                End If
            Next
        End If
        Dim bytes As Byte() = New WebClient().DownloadData(If(CheckBox3.Checked, "http://finviz.com/chart.ashx?t=" & TextBox1.Text & "&ty=c&ta=0&p=d&s=l", "http://finviz.com/chart.ashx?t=" & TextBox1.Text & "&ty=c&ta=1&p=d&s=l"))
        Dim ms As New MemoryStream(bytes)
        PictureBox1.Image = Image.FromStream(ms)
        names.Add(TextBox1.Text)
        stockbytes.Add(bytes)
        totalStocks += 1
        currentIndex = totalStocks - 1
        Label2.Text = currentIndex + 1 & "/" & totalStocks
        Label1.Text = "Stock: " & names(currentIndex)
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If currentIndex > 0 Then
            currentIndex -= 1
            PictureBox1.Image = Image.FromStream(New MemoryStream(DirectCast(stockbytes(currentIndex), Byte())))
        End If
        Label2.Text = currentIndex + 1 & "/" & totalStocks
        Label1.Text = "Stock: " & names(currentIndex)
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        If currentIndex < totalStocks - 1 Then
            currentIndex += 1
            PictureBox1.Image = Image.FromStream(New MemoryStream(DirectCast(stockbytes(currentIndex), Byte())))
        End If
        Label2.Text = currentIndex + 1 & "/" & totalStocks
        Label1.Text = "Stock: " & names(currentIndex)
    End Sub

    Private Sub CheckBox3_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox3.CheckedChanged
        stockbytes.Clear()
        For i As Integer = 0 To names.Count - 1
            stockbytes.Add(New WebClient().DownloadData(If(CheckBox3.Checked, "http://finviz.com/chart.ashx?t=" & DirectCast(names(i), String) & "&ty=c&ta=0&p=d&s=l", "http://finviz.com/chart.ashx?t=" & DirectCast(names(i), String) & "&ty=c&ta=1&p=d&s=l")))
        Next
        If stockbytes.Count - 1 > 0 Then PictureBox1.Image = Image.FromStream(New MemoryStream(DirectCast(stockbytes(currentIndex), Byte())))
    End Sub

    Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        OpenFileDialog1.InitialDirectory = "%CSIDL_DEFAULT_MYDOCUMENTS%"
        OpenFileDialog1.Filter = "StockViewer Files(*.stkvwr)|*.stkvwr"
        OpenFileDialog1.ShowDialog()

        Try
            Dim sr As New StreamReader(OpenFileDialog1.FileName)
            Dim stocks As String() = sr.ReadToEnd().Split(",")
            stockbytes.Clear()
            For i As Integer = 0 To stocks.Length - 1
                stockbytes.Add(New WebClient().DownloadData(If(CheckBox3.Checked, "http://finviz.com/chart.ashx?t=" & stocks(i) & "&ty=c&ta=0&p=d&s=l", "http://finviz.com/chart.ashx?t=" & stocks(i) & "&ty=c&ta=1&p=d&s=l")))
                totalStocks += 1
                names.Add(stocks(i))
            Next
            currentIndex = 0
            PictureBox1.Image = Image.FromStream(New MemoryStream(DirectCast(stockbytes(currentIndex), Byte())))
            Label2.Text = currentIndex + 1 & "/" & totalStocks
            Label1.Text = "Stock: " & names(currentIndex)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFileDialog1.InitialDirectory = "%CSIDL_DEFAULT_MYDOCUMENTS%"
        SaveFileDialog1.Filter = "StockViewer Files(*.stkvwr)|*.stkvwr"
        SaveFileDialog1.ShowDialog()
        Dim sr As New StreamWriter(SaveFileDialog1.FileName)
        For stock As Integer = 0 To names.Count - 1
            sr.Write(DirectCast(names(stock), String) & ",")
        Next
        sr.Close()
    End Sub
End Class
